from common.utils.rights import permission_required, view_logging_required
from common.utils.files_read import files_read
from . import index_bp
import cv2
from flask import render_template,request
from flask_wtf.form import CombinedMultiDict
from models.form import UploadForm


@index_bp.get('/add_pro')
@index_bp.post('/add_pro')
@view_logging_required
@permission_required("add:pro")
def add_pro():
    if request.method == "GET":
        return render_template('admin/cv/add.html')
    else:
        form = UploadForm(CombinedMultiDict([request.form, request.files]))
        if form.validate():
            f1 = request.files['file']  # 获取文件流
            f2 = request.files['file2']  # 获取文件流
            url =  files_read(f1,f2)
            file_url = url[0]
            file_url2 =url[1]
            #图像处理
            pic1 = cv2.imread(file_url)
            pic2 = cv2.imread(file_url2)
            # 相加
            result = cv2.addWeighted(pic1, 1, pic2, 0.2, 0)
            res_pic = "static/download/add.jpg"
            cv2.imwrite(res_pic, result)
            return render_template('admin/cv/add.html', pic1=file_url, pic2=file_url2, res_pic=res_pic)
        else:
            return "only support jpg,png,gif"